package com.jwaoo.chat.session.avchat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.dx.as.UI;
import com.dx.as.common.string.StringUtil;
import com.jwaoo.chat.ChatConstants;
import com.jwaoo.chat.Message;
import com.jwaoo.chat.R;
import com.jwaoo.chat.data.db.MessageHandler;
import com.jwaoo.chat.session.Extras;

import org.appspot.apprtc.WebRtcClient;
import org.webrtc.SurfaceViewRenderer;

import static com.jwaoo.chat.session.avchat.AVChatSoundPlayer.RingerTypeEnum.CONNECTING;
import static com.jwaoo.chat.session.avchat.AVChatSoundPlayer.RingerTypeEnum.RING;

public class AVChatActivity extends UI {
    private SurfaceViewRenderer pipRenderer;
    private SurfaceViewRenderer fullscreenRenderer;
    private WebRtcClient webRtcClient;
    private final String roomUrl = "http://192.168.10.201:8080";
    private String roomId;
    private int friendId;
    private boolean isComming = false;
    private View video;
    private View audio;
    private View chatView;
    private View watting;
    private View avchat_video_logout;
    public static void launch(Context context, int friend, int avChatType,String roomId){
        Intent intent = new Intent(context,AVChatActivity.class);
        intent.putExtra(Extras.EXTRA_ACCOUNT,friend);
        intent.putExtra(Extras.EXTRA_ROOM_ID,roomId);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private void parseIntent(){
        roomId = getIntent().getStringExtra(Extras.EXTRA_ROOM_ID);
        isComming = true;
        if (StringUtil.isEmpty(roomId)){
            roomId = WebRtcClient.ROOM_REQUEST_ID;
            isComming = false;
        }
        friendId = getIntent().getIntExtra(Extras.EXTRA_ACCOUNT,0);
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_avchat);
        parseIntent();
        audio = findViewById(R.id.audio_option);
        video = findViewById(R.id.video_option);
        chatView = findViewById(R.id.chating);
        watting = findViewById(R.id.watting);
        avchat_video_logout = findViewById(R.id.avchat_video_logout);
        avchat_video_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("===","===");
                AVChatSoundPlayer.instance().stop();
                webRtcClient.hangUp();
                finish();
            }
        });
        findViewById(R.id.btn_refuse).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("===","refuse");
                AVChatSoundPlayer.instance().stop();
                webRtcClient.hangUp();
                finish();
            }
        });

        findViewById(R.id.btn_receive).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("===","receive");
                        watting.setVisibility(View.GONE);
                        chatView.setVisibility(View.VISIBLE);
                        video.setVisibility(View.VISIBLE);
                        AVChatSoundPlayer.instance().stop();
                        webRtcClient.call();
                    }
                });
            }
        });


        if (isComming) {
            chatView.setVisibility(View.GONE);
            video.setVisibility(View.GONE);
            watting.setVisibility(View.VISIBLE);
            AVChatSoundPlayer.instance().play(RING);
        }else {
            AVChatSoundPlayer.instance().play(CONNECTING);
            chatView.setVisibility(View.VISIBLE);
        }
        pipRenderer = (SurfaceViewRenderer) findViewById(R.id.pip_video_view);
        fullscreenRenderer = (SurfaceViewRenderer) findViewById(R.id.fullscreen_video_view);
        webRtcClient = new WebRtcClient(this,roomUrl,roomId, pipRenderer, fullscreenRenderer);
        if (!isComming){
            AVChatSoundPlayer.instance().stop();
            webRtcClient.call();
        }
    }

    private WebRtcClient.RtcCallStateChange rtcCallStateChange = new WebRtcClient.RtcCallStateChange() {
        @Override
        public void onHangUp() {
            Log.e("===","onHangUp");
            webRtcClient.setRtcCallStateChange(null);
            webRtcClient.hangUp();
            finish();
        }

        @Override
        public void onReportError(String s) {
            Log.e("===","onReportError : "+s);
        }

        @Override
        public void onDataMessage(String s) {
            Log.e("===","onDataMessage : "+s);
        }

        @Override
        public void onConnectedToRoom(boolean b, String s) {

            if (!isComming){
                Log.e("==="+b,"onConnectedToRoom : "+s);
                if (b){
                    Message message = MessageHandler.getInstace(getApplicationContext()).createMessage(ChatConstants.video_chat,friendId,s);
                    MessageHandler.getInstace(getApplicationContext()).sendMessage(message);
                }
            }
        }
    };

    // Activity interfaces
    @Override
    public void onStop() {
        super.onStop();
        webRtcClient.setRtcCallStateChange(null);
        webRtcClient.hangUp();
    }

    @Override
    public void onStart() {
        super.onStart();

        webRtcClient.setRtcCallStateChange(rtcCallStateChange);

    }

    @Override
    public void onBackPressed() {
        AVChatSoundPlayer.instance().stop();
        if (webRtcClient != null){
            webRtcClient.setRtcCallStateChange(null);
            webRtcClient.hangUp();
        }
        finish();
    }
}
